package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.dto.EditUserDTO;
import com.hr.domain.dto.UserDTO;
import com.hr.domain.po.User;
import com.hr.domain.query.UserPageResult;
import com.hr.domain.query.UserQueryParams;
import com.hr.domain.vo.ClientUserInfoVO;
import com.hr.domain.vo.UserVO;
import com.hr.service.UserService;
import com.hr.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
* @author 86150
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-09-22 15:58:46
*/

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService{

    private final UserMapper userMapper;


    /**
     * 获取所有用户
     *
     * @return
     */
    @Override
    public List<UserVO> getAllUsers() {
         return userMapper.getAllUsers();
    }

    /**
     * 获取用户分页列表
     *
     * @param queryParams
     * @return UserPageResult
     */
    @Override
    public UserPageResult getUserPageList(UserQueryParams queryParams) {
        //开始分页（pageHelper）
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        //传入分页参数
        List<UserVO> userList = userMapper.getUserPageList(queryParams);
        //转换为page<>类型
        Page<UserVO> page = (Page<UserVO>) userList;
        //返回结果
        return new UserPageResult(page.getTotal(), page.getResult());
    }

    /**
     * 禁用、启用账户
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public String updateUserStatus(Long id, Integer status) {
        int row = userMapper.updateUserStatus(id, status);
        if (row > 0){
            //更新时间
           User user = userMapper.SelectById(id);
           user.setUpdateTime(LocalDateTime.now());
        }
        return row > 0 ? "更新账户状态成功" : "更新账户状态失败";
    }

    /**
     * 新增用户
     *
     * @param userDTO
     * @return msg
     */
    @Override
    public String addUser(UserDTO userDTO) {


        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(userDTO.getUsername());
        if (existingUser != null) {
            return "用户名已存在";
        }

        // 使用MD5加密密码
        String encodedPassword = DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes());

        // 创建User对象
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setPassword(encodedPassword); // 设置加密后的密码
        // 生成18位随机身份证号
        StringBuilder idCard = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 18; i++) {
            idCard.append(random.nextInt(10)); // 生成 0-9 的随机数字
        }
        user.setIdCard(idCard.toString());

        int row = userMapper.addUser(user);
        return row > 0 ? "新增用户成功" : "新增用户失败";
    }

    /**
     * 获取单条用户信息（查询回显）
     *
     * @param id
     * @return
     */
    @Override
    public UserVO SelectById(Long id) {
        UserVO userVO = userMapper.OtherSelectById(id);

        if (userVO != null){
            return userVO;
        }
        return null;
    }

    @Override
    public String updateUser(EditUserDTO editUserDTO) {
        User user = new User();
        BeanUtils.copyProperties(editUserDTO, user);


        //使用MD5加密密码
        String encodedPassword = DigestUtils.md5DigestAsHex(editUserDTO.getPassword().getBytes());
        user.setPassword(encodedPassword);
        //设置更新时间
        user.setUpdateTime(LocalDateTime.now());
        int row = userMapper.update(user);
        return row > 0 ? "更新用户信息成功" : "更新用户信息失败";
    }

    /**
     * 获取用户信息
     *
     * @return ClientUserInfoVO
     */
    @Override
    public ClientUserInfoVO getUserInfo(Long id) {

        if (id == null){
            return null;
        }

        ClientUserInfoVO clientUserInfo = userMapper.selectById(id);

        if (clientUserInfo != null){
            return clientUserInfo;
        }
        return null;
    }
}




